package com.wenkaihu.android.chess.pc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.wenkaihu.android.chess.model.PaceblePoint;

public class PCServer {
	private static final int SERVERPORT = 8888;
	private static List<Socket> mClientList = new ArrayList<Socket>();
	private ExecutorService mExecutorService;
	private ServerSocket mServerSocket;

	public PCServer() {
		try {
			mServerSocket = new ServerSocket(SERVERPORT);
			mExecutorService = Executors.newCachedThreadPool();
			System.out.println("PCServer #Start the Server ...");
			Socket client = null;
			while (true) {
				// listener for the client connect in
				client = mServerSocket.accept();
				System.out.println("PCServer #Get a New Client ...");
				mClientList.add(client);
				mExecutorService.execute(new ThreadServer(client));
			}
		} catch (IOException e) {
			System.out.println("PCServer #Get a IOException " + e);
		} catch (ClassNotFoundException e) {
			System.out.println("PCServer #Get a ClassNotFoundException " + e);
		}
	}

	public static void main(String[] args) {
		// call the constructor method !
		new PCServer();
	}

	class ThreadServer implements Runnable {
		private Socket mClient;
		private BufferedReader mBufferedReader;
		private PrintWriter mPrintWriter;
		private String mStrMSG;
		private PaceblePoint mPoint;
		private List<PaceblePoint> mPointList = new ArrayList<PaceblePoint>();

		public ThreadServer(Socket client) throws IOException, ClassNotFoundException {
			this.mClient = client;
			mPoint = (PaceblePoint) (new ObjectInputStream(client.getInputStream())).readObject();
			
			System.out.println("PCServer # Receive Point info is#" + mPoint);
			
			mPointList.add(mPoint);
			
			(new ObjectOutputStream(client.getOutputStream())).writeObject(mPoint);
			
			if (mClientList.indexOf(client) == mClientList.size()) {
				(new ObjectOutputStream(mClientList.get(0).getOutputStream())).writeObject(mPoint);
			}else{
				(new ObjectOutputStream(mClientList.get(mClientList.size()).getOutputStream())).writeObject(mPoint);
			}
			//sendMessage();
		}

		public void run() {
//			try {
//				while ((mStrMSG = mBufferedReader.readLine()) != null) {
//					if (mStrMSG.trim().equals("exit")) {
//						mClientList.remove(mClient);
//						mBufferedReader.close();
//						mPrintWriter.close();
//						mStrMSG = "user:" + this.mClient.getInetAddress()
//								+ " exit total:" + mClientList.size();
//						mClient.close();
//						sendMessage();
//						break;
//					} else {
//						mStrMSG = mClient.getInetAddress() + ":" + mStrMSG;
//						sendMessage();
//					}
//				}
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
		}

		private void sendMessage() throws IOException {
			System.out.println(mStrMSG);
			for (Socket client : mClientList) {
				mPrintWriter = new PrintWriter(client.getOutputStream(), true);
				mPrintWriter.println(mStrMSG);
			}
		}
	}
}
